package com.pay.download;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.pay.entity.ZfbMember;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

/**
 * fundAccountDetail下载器
 * @author Administrator
 *
 */
public class FundAccountDetailDownload {

	public static String download(ZfbMember zfbMember) throws Exception {
		CloseableHttpClient httpClient=HttpClients.createDefault(); // 创建httpClient实例
		HttpPost httpget = new HttpPost("https://mbillexprod.alipay.com/enterprise/fundAccountDetail.json"); // 创建httpget实例
		httpget.addHeader("Cookie", zfbMember.getCookie());
		httpget.addHeader("Origin","https://mbillexprod.alipay.com");
		httpget.addHeader("Referer","https://mbillexprod.alipay.com/enterprise/fundAccountDetail.htm");
		httpget.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 QQBrowser/4.4.116.400 Version/5.1.1 Safari/534.51.22");
		httpget.addHeader("X-Requested-With", "XMLHttpRequest");
		List<NameValuePair> formParams = null ;
		formParams = new ArrayList<NameValuePair>();
		formParams.add(new BasicNameValuePair("queryEntrance", "1"));
		formParams.add(new BasicNameValuePair("billUserId", zfbMember.getUserId()));
		formParams.add(new BasicNameValuePair("showType", "1"));
		formParams.add(new BasicNameValuePair("type", ""));
		formParams.add(new BasicNameValuePair("precisionQueryKey", "tradeNo"));
		Date startDateInput = DateUtil.offsetDay(new Date(), -1) ;
		String startDateInputStr = DateUtil.format(startDateInput, "yyyy-MM-dd 00:00:00") ;
		formParams.add(new BasicNameValuePair("startDateInput", startDateInputStr));
		DateTime date = DateUtil.offsetDay(new Date(), 1);
		String endDateInput = DateUtil.format(date, "yyyy-MM-dd 00:00:00") ;
		formParams.add(new BasicNameValuePair("endDateInput", endDateInput));
		formParams.add(new BasicNameValuePair("pageSize", "20"));
		formParams.add(new BasicNameValuePair("pageNum", "1"));
		formParams.add(new BasicNameValuePair("total", "10000"));
		formParams.add(new BasicNameValuePair("sortTarget", "tradeTime"));
		formParams.add(new BasicNameValuePair("order", "descend"));
		formParams.add(new BasicNameValuePair("sortType", "0"));
		formParams.add(new BasicNameValuePair("_input_charset", "gbk"));
		formParams.add(new BasicNameValuePair("ctoken", zfbMember.getCtoken()));
		HttpEntity requestEntity = new UrlEncodedFormEntity(formParams, "UTF-8");
		httpget.setEntity(requestEntity);
		CloseableHttpResponse response = httpClient.execute(httpget);
		HttpEntity entity=response.getEntity(); // 获取返回实体
		String result = EntityUtils.toString(entity, "utf-8");
		response.close(); 
		httpClient.close();
		return result ;
	}
}
